package com.rico.kok.controller.user;

import com.rico.kok.entity.User;
import com.rico.kok.service.UserServiceImpl;
import com.rico.kok.service.interf.UserService;
import com.rico.kok.tool.RandomCreate;
import com.rico.kok.tool.RegisterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class ForgetPasswordController {

    @Autowired
    UserService userService;

    @Autowired
    RegisterUtils registerUtils;

    @Autowired
    JavaMailSenderImpl mailSender;

    @RequestMapping("/forgetPassword.html")
    public String toForgetPassword(){
        return "forgetPassword";
    }

    @PostMapping("/forgetPassword.htm")
    public String forgetPassword(User user, @RequestParam("verifyCode") String verifyForgetCode, HttpSession session, Model model){
        //确定用户不为空
        System.out.println(user);
        if (user!=null){
            System.out.println(1);
            //确定用户邮箱不为空
            if (user.getUserEmail()!=null&&!"".equals(user.getUserEmail())){
                System.out.println(2);
                List<User> list= userService.selectUsers(user);
                //验证用户是否存在
                if (list.size()<=0){
                    setMsg(model,"错误","邮箱不存在","error");
                    return "msg";
                }
                //验证验证码是否发送
                if (session.getAttribute("forgetVerifyCode")==null){
                    setMsg(model,"错误","你没有发送验证码","error");
                    return "msg";
                }
                //判断验证码是否过期
                if (!registerUtils.isOutTime("forgetSendTime",session)) {
                    setMsg(model, "失败", "验证码过期了！", "error");
                    return "msg";
                }
                //验证验证码是否正确
                if (!session.getAttribute("forgetVerifyCode").toString().equals(verifyForgetCode)){
                    setMsg(model,"错误","验证码错误！","error");
                    return "msg";
                }
                //验证是否是收件邮箱
                if (!session.getAttribute("forgetEmail").equals(user.getUserEmail())){
                    setMsg(model,"错误","不是发送验证码的邮箱！","error");
                    return "msg";
                }
                //校验密码是否为空
                if (user.getUserPassword()==null||"".equals(user.getUserPassword())) {
                    setMsg(model, "失败", "密码不能为空！", "error");
                    return "msg";
                }
                //校验密码长度
                if (user.getUserPassword().length() > 50) {
                    setMsg(model, "失败", "这么长的密码？没必要吧？", "error");
                    return "msg";
                }
                System.out.println(3);
                User updateUser=new User();
                updateUser.setUserId(list.get(0).getUserId());
                updateUser.setUserPassword(user.getUserPassword());
                System.out.println(updateUser);
                userService.updateUser(updateUser);
                setMsg(model,"成功","密码修改成功了","success");
                return "msg";
            }
        }
        setMsg(model,"错误","密码修改失败了","error");
        return "msg";
    }

    @PostMapping("/sendForgetEmail.htm")
    @ResponseBody
    public void sendEmail(HttpServletRequest request){
        //获取邮箱地址
        String userEmail = request.getParameter("email");
        //获取6位数随机数
        String checkCode = RandomCreate.generateVerCode();
        //判断是否发送过验证码
        if (request.getSession().getAttribute("forgetVerifyCode") != null) {
            Date sendTime = (Date) request.getSession().getAttribute("forgetSendTime");
            int res = RandomCreate.getMinute(sendTime, new Date());
            if (res < 1) {
                return;
            }
        }
        request.getSession().setAttribute("forgetEmail", userEmail);
        request.getSession().setAttribute("forgetVerifyCode", checkCode);
        request.getSession().setAttribute("forgetSendTime", new Date());
        registerUtils.sendEmail(userEmail, checkCode, mailSender, "forgetPassword.properties");
//        System.out.println("找回密码验证码："+checkCode);
    }

    //验证发送验证码间隔是否超过1分钟
    @PostMapping("/verifyForgetSendTime.htm")
    @ResponseBody
    public boolean sendTime(HttpServletRequest request) {
        //判断验证码是否发送过
        if (request.getSession().getAttribute("forgetVerifyCode") != null) {
            //发送过后判断发送间隔是否超过一分钟
            Date date = (Date) request.getSession().getAttribute("forgetSendTime");
            int res = RandomCreate.getMinute(date, new Date());
            if (res < 1) {
                return false;
            }
        }
        return true;
    }

    //设置msg消息
    public void setMsg(Model model, String title, String msg, String code) {
        //向msg传值
        model.addAttribute("title", title);
        model.addAttribute("msg", msg);
        model.addAttribute("code", code);
    }
}
